×
☰ See All Chapters

Spring Description Annotation

Spring 4.0 provided @Description annotation to add a textual description to bean definitions derived from @Component or @Bean. @Description has value attribute which accepts the textual description to associate with the bean definition.

What is the real advantage from @Description annotation where we can add comments in java and attach useful information with code?

When we add comments, it will not popup in editors and will not convey information quickly. Developer should open actual file and should read the commented information. But @Description conveys the information quickly to develop. When developer mouse hover on the bean class, he can see the description without opening those files. Editors display the description in a dialog window. In our below example we have annotated Employee class with @Description.  From Demo class where we are using the Employee bean, we can see that our editor displaying description in a dialog. This is really essential feature for developer to get the information quickly when working on real time projects involving thousands of java beans.

spring-description-annotation-0
 

package com.java4coding;

 

import org.springframework.context.annotation.Description;

import org.springframework.stereotype.Component;

 

@Component

@Description("This is Employee Bean. printEmployeeName() prints employee name.")

public class Employee{

        public void printEmployeeName(String name){

                System.out.println(name);

        }

}

 

package com.java4coding;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

 

public class Demo {

        public static void main(String[] args) {

                AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.java4coding");

                Employee employee = (Employee) context.getBean("employee");

                employee.printEmployeeName("Manu Manjunatha");

                ((ConfigurableApplicationContext) context).close();

        }

}

 


All Chapters
Author